home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5705 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Optimization Q: for( i=0; i<SIZE/2; i++) ??
  5. Date: Tue, 20 Feb 96 22:18:32 GMT
  6. Organization: none
  7. Message-ID: <824854712snz@genesis.demon.co.uk>
  8. References: <4g1j9n$ooe@news.csus.edu> <Dn2nKB.C1@uns.bris.ac.uk>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <Dn2nKB.C1@uns.bris.ac.uk>
  15.            nathan@pact.srf.ac.uk "Nathan Sidwell" writes:
  16.  
  17. >Jerry Leong (wleong@sfsu.edu) wrote:
  18. >
  19. >: This question has been bothering me for quite a while now.
  20. >
  21. >: If I have the following for loop,
  22. >:         for( i=0; i< SIZE/2 ; i++).....
  23. >: Will the code execute faster if I precompute SIZE/2 before hand and
  24. >: do this
  25. >:       k = SIZE/2;
  26. >:        for(i=0; i<k ; i++).....
  27.  
  28. ...
  29.  
  30. >Now, writing the loop as
  31. >for(ix = SIZE/2; ix--;)
  32. > ...
  33. >might make a (small) difference.
  34.  
  35. Like making the loop variable take on the values (within the loop body) of
  36. SIZE/2 down to 1 rather than 0 up to SIZE/2-1. :-)
  37.  
  38. If the variable is just used as an iteration count and its value is not
  39. important then this is fine but the compiler can probably make this
  40. optimisation itself in that case (I've seen it done).
  41.  
  42. -- 
  43. -----------------------------------------
  44. Lawrence Kirby | fred@genesis.demon.co.uk
  45. Wilts, England | 70734.126@compuserve.com
  46. -----------------------------------------
  47.